package struct.scene.ui
{
	import collection.DataCollection;
	
	import com.common.displayObjects.ComponentChildren;
	
	import data.MapVO;
	
	import flash.display.Scene;
	import flash.geom.Point;
	
	import struct.scene.SceneModule;
	import struct.scene.items.Grid;
	import struct.scene.items.MapItem;
	import struct.scene.items.Player;
	
	/**
	 * 场景的地格 层 
	 * @author Administrator
	 * 
	 */	
	public class SceneGridlayer extends ComponentChildren
	{
		public var arrGrid:Array;
		
		public function SceneGridlayer(width:int, height:int)
		{
			super(width, height);
			arrGrid = [];
		}
		
		public function initGridlayer(arrGridInfo:Array, mapVO:MapVO):void
		{
			var len:int = arrGridInfo.length;
			for (var i:int; i< len; i++)
			{
				var g:Grid = new Grid(mapVO.gridWidth, mapVO.gridHeight, i%mapVO.columnNum, (i/mapVO.columnNum) >>0, arrGridInfo[i].isObstacle, arrGridInfo[i]);
				g.x = g.width * g.xIndex;
				g.y = g.height * g.yIndex;
				addChild(g);
				arrGrid.push(g);
				// 添加物品
				if (arrGridInfo[i].itemVO)
				{
					var mapItem:MapItem = new MapItem(arrGridInfo[i].itemVO);
					g.addItem(mapItem);
				}
			}
		}
		
		/**
		 * 获取目标位置的grid对象 
		 * @param xIndex
		 * @param yIndex
		 * @return 
		 * 
		 */		
		public function getGrid(xIndex:int, yIndex:int):Grid
		{
			if (xIndex < 0 || yIndex < 0)return null;
			for (var i:int; i<arrGrid.length; i++)
			{
				if (arrGrid[i].xIndex == xIndex && arrGrid[i].yIndex == yIndex)
					var g:Grid = arrGrid[i];
					continue;	
			}
			return g;
		}
		
		/**
		 *	根据初始格子的 xindex和yindex  获取指定方向下的最近的格子 
		 *  如果这个格子未找到  表示就是当前人物所占用的格子  返回null
		 * @param startXIndex		起始格子的xIndex
		 * @param startYIndex		起始格子的yIndex
		 * @param direction			查找方向   1.2.4.8  上右下左
		 * 
		 */		
		public function getEdgeGrid(startXIndex:int, startYIndex:int, searchDirection:int):Grid
		{
			var targetGrid:Grid;
			switch (searchDirection)
			{
				case 1: //上
						var g:Grid = getGrid(startXIndex, --startYIndex);
						if (g&&g.isRoad){
							targetGrid = g;
						}
						break;
				case 2://右
						g = getGrid(++startXIndex, startYIndex);
						if (g&&g.isRoad){
							targetGrid = g;
						}
						break;
				case 4: //下
						g = getGrid(startXIndex, ++startYIndex);
						if (g&&g.isRoad){
							targetGrid = g;
						}
							break;
				case 8://左
						g = getGrid(--startXIndex, startYIndex);
						if (g&&g.isRoad){
							targetGrid = g;
						}
						break;
			}
			return targetGrid;
		}
		
		public function checkItems(player:Player):void
		{
//			if (!player.isUser)return;
			if (!DataCollection.gameStart)return;
			var stillHasItem:Boolean = false; //场景中的物品已经全部用完
			for (var i:int; i<arrGrid.length; i++)
			{
				var g:Grid = arrGrid[i];
				if (g.hasMapItem())
				{
					if (player.testBorder.hitTestObject(g.mapItem.testBorder))
					{
						//碰撞
						if (g.mapItem.id == "2004"){
							//传送点
							var index:int = g.gridVO.flyTo;
							var xIndex:int =  index%SceneModule.getInstance().mapVO.columnNum;
							var yIndex:int = (index/SceneModule.getInstance().mapVO.columnNum) >>0;
							
							var grid:Grid = getGrid(xIndex, yIndex);
							player.x = grid.x + grid.width * .5;
							player.y = grid.y + grid.height * .5;
							player.updatePosition(); //更新player
							if (!player.isUser)
							{
								//ai
								player.stop();
								player.move(getAINextGrid(player.xIndex, player.yIndex));
							}
						}else
						{
							//普通物品
							if (player.isUser)
								SceneModule.getInstance().useMapItem(g.removeitem());
						}
					}else{
						//未碰撞
						if (g.mapItem.id != "2004"){
							stillHasItem = true;
						}
					}
 				}
			}
			
			if (!stillHasItem)
			{
				//所有物品都被清除
				SceneModule.getInstance().missionComplete();
			}
		}
		
		/**
		 * 获取ai的下一个移动目标格子
		 * @param startXIndex		ai所在的xIndex
		 * @param startYIndex		ai所在的yIndex
		 * 
		 */		
		public function getAINextGrid(startXIndex:int, startYIndex:int):Grid
		{
			var randomRange:int = (1 + DataCollection.AIRANGE * Math.random()) >>0;
			var arrGrid:Array = [];
			 // 查找上
			var g:Grid, perhapsGrid:Grid;
			for (var i:int = 1; i <= randomRange; i++)
			{
				g = getGrid(startXIndex, startYIndex - i);
				if (g && g.isRoad){
					perhapsGrid = g;
				}else
				{
					i = randomRange;
				}
			}
			if (perhapsGrid )arrGrid.push(perhapsGrid)
			//查找右
			perhapsGrid = null;
			for (i = 1; i <= randomRange; i++)
			{
				g = getGrid(startXIndex + i, startYIndex);
				if (g && g.isRoad){
					perhapsGrid = g;
				}else
				{
					i = randomRange;
				}
			}
			if (perhapsGrid )arrGrid.push(perhapsGrid)
			//查找下
			perhapsGrid = null;
			for (i = 1; i <= randomRange; i++)
			{
				g = getGrid(startXIndex, startYIndex + i);
				if (g && g.isRoad){
					perhapsGrid = g;
				}else
				{
					i = randomRange;
				}
			}
			if (perhapsGrid )arrGrid.push(perhapsGrid)
			//查找左
			perhapsGrid = null;
			for (i = 1; i <= randomRange; i++)
			{
				g = getGrid(startXIndex - i, startYIndex);
				if (g && g.isRoad){
					perhapsGrid = g;
				}else
				{
					i = randomRange;
				}
			}
			if (perhapsGrid )arrGrid.push(perhapsGrid)
			
			var index:int = (Math.random() * arrGrid.length) >> 0;
			return arrGrid[index];
		}
	}
}